﻿using Bll;
using Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils;


namespace Vote.Filters
{
    public class LoginFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 动作执行之前先获取用户信息
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Session["OpenId"] != null)
            {
                //去获取用户的openId
                bool flag = false;

                #region 先从缓存读取,查看是否有对应的用户
                string openId = HttpContext.Current.Session["OpenId"].ToString();
                var redisClient = RedisManager.GetClient();
                var userInfoModelList = redisClient.GetTypedClient<UserInfoForVote>();
                List<UserInfoForVote> listUserInfo = userInfoModelList.GetAll().ToList();
                foreach (UserInfoForVote model in listUserInfo)
                {
                    if (!string.IsNullOrEmpty(model.OpenId)&&model.OpenId.Equals(openId))
                    {
                        HttpContext.Current.Session["userName"] = model.UserName;
                        flag = true;
                        break;
                    }
                }
                #endregion

                if (flag)
                {
                    HttpContext.Current.Response.Redirect("http://vote.gxanr.com");
                }
            }

            base.OnActionExecuting(filterContext);
        }
    }
}